package com.yiji.fintech.musfinance.common.enginerules.rules;

import com.acooly.core.utils.Collections3;
import com.acooly.core.utils.Dates;
import com.yiji.fintech.musfinance.common.annotation.RuleNameAnnotation;
import com.yiji.fintech.musfinance.common.enginerules.base.RulesFilter;
import com.yiji.fintech.musfinance.common.domain.channel.ChannelDomain;
import com.yiji.fintech.musfinance.entity.TransactionStatistics;
import com.yiji.fintech.musfinance.service.TransactionStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by ouwen@yiji.com} on 2017/9/22.
 */
@Service
@RuleNameAnnotation("日限规则服务")
public class BaseFilter extends RulesFilter {
    @Autowired
    private TransactionStatisticsService statisticsService;

    @Override
    public void filter(ChannelDomain domain) {
        List<TransactionStatistics> transactionStatistics = statistics(domain);
        if(Collections3.isNotEmpty(transactionStatistics)){
            TransactionStatistics ts = transactionStatistics.get(0);
            Long moneysun = ts.getMoneySum();
            if(moneysun != null){
                channelApiList = channelApiList.stream().filter((s)->s.getDayQuota().getCent()<moneysun.longValue()).collect(Collectors.toList());
            }
        }
    }

    /**
     * 统计当天交易
     * @param domain
     */
    private List<TransactionStatistics> statistics(ChannelDomain domain){
        TransactionStatistics statistics = new TransactionStatistics();
        statistics.setPartnerId(domain.getPartnerId());
        statistics.setGatewayChannelApi(domain.getGatewayChannelApi());
        statistics.setInstBankId(domain.getInstBankId());
        statistics.setSumDate(Dates.format(new Date(),Dates.CHINESE_DATE_FORMAT_LINE));//统计当天
        List<TransactionStatistics> transactionStatistics = statisticsService.statistic(statistics);
        return transactionStatistics;
    }
}
